import axios from "axios";

export default {
    namespaced: true,
    state: {
        pageData:{
            currentPage: 1,
            pageSize: 5,
            totalPage: 1,
            totalCount: 1,
            goods: []
        },
        choice: "",
        searchFor: "",
        changed: "",
    },
    getters:{
        newInfo(state){
            if(state.searchFor){
                return { ...state.pageData , goods: state.pageData.goods.filter(item => item[state.choice].includes(state.searchFor))};
            }else{
                return state.pageData
            }
        }
    },
    mutations: {
        getAdress(state, payload){
            state.pageData = payload;
        },
        searchAdress(state, payload){
            state.choice = payload.choice;
            state.searchFor = payload.searchFor;
        },
        modifyAddress(state, payload){
            console.log("state.choice11111", state.choice)
            state.changed = payload;
            console.log("state.choice2222", state.choice)
        }
    },
    actions: {
        getAdressAsync(context){
            axios.get("/api/addresses/getAdresses", { params: context.state.pageData }).then(res => {
                context.commit("getAdress", res.data);
            })
        },
        modifyAddressAsync(context, payload){
            console.log(payload);
            axios.post("/api/addresses/modifyAddress", payload).then(res => {
                console.log(res.data);
                context.commit("modifyAddress", res.data);
            })
        }
    },
    modules: {
    }
}